package cn.edu.zjgsu.ito.controller;
//new
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import cn.edu.zjgsu.ito.service.AdminUploadPreScoreService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

public class AdminUploadPreScoreController extends Controller {
	
	/**
	 * 历史成绩导入
	 * 杨昆鹏
	 * */
	
	private AdminUploadPreScoreService adminUploadPreScoreService =ServiceFactory.createAdminUploadPreScoreService();
	
	public void uploadPreScore(){
		
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try
		{
			UploadFile file=getFile("uploadFile");
			String fileName=file.getFileName();
			String extension=fileName.substring(fileName.lastIndexOf("."));
			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source=file.getFile();
				String newFileName=System.currentTimeMillis()+extension;
				FileInputStream fis=new FileInputStream(source);
				File targetDir=new File(filedir);
				File target=new File(targetDir,newFileName);
				FileOutputStream fos=new FileOutputStream(target);
				byte[] bts=new byte[300];
				while (fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				String abc = filedir+"\\"+newFileName;
				
				InputStream verifyData = new FileInputStream(abc);
				String temp = "";
				ArrayList<String> uploadStatus = (ArrayList<String>) adminUploadPreScoreService.verifyData(verifyData);
				for(int i = 0;i < uploadStatus.size();i++){
				temp += uploadStatus.get(i)+",";
				}
				temp = temp.substring(0,temp.length()-1);
				setAttr("uploadInf",temp);
				fos.close();
				fis.close();
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
//				renderFreeMarker("/WEB-INF/param/param.html");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	private void index() {
		// TODO Auto-generated method stub
		render("scoremanage.html");
//		System.out.println("咚咚咚");
		
	}



	public void fileDownload()
	{
		String fileName="学生历史成绩模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;
		renderFile(new File(path));
	}
	

}
//new
